package Student;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {

    private JButton bn_logion = new JButton("登录");
    private JButton bn_reset = new JButton("重置");
    private JTextField tx_account = new JTextField(10);
    private JTextField tx_password = new JPasswordField(10);

    // 声明屏幕的宽高，程序窗口的宽高
    private int windowWidth;
    private int windowHeight;
    private int screenSizeWidth;
    private int screenSizeHeight;

    public LoginFrame() {
        this.setSize(300, 180); // 设置窗体大小
        // 给屏幕的宽度高度，程序窗口的宽度高度赋值
        Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
        screenSizeWidth = (int) dimension.getWidth();
        screenSizeHeight = (int) dimension.getHeight();
        windowWidth = this.getWidth();
        windowHeight = this.getHeight();
        // 设置程序窗口的位置为屏幕的正中央
        this.setLocation((screenSizeWidth-windowWidth)/2, (screenSizeHeight-windowHeight)/2);
        this.setTitle("登录"); // 设置窗体标题栏
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体默认关闭事件
        this.setResizable(false);
        this.setLayout(new FlowLayout()); // 设置布局管理器
        JPanel p1 = new JPanel();
        p1.add(new JLabel("账号："));
        p1.add(tx_account);
        this.getContentPane().add(p1);
        JPanel p2 = new JPanel();
        p2.add(new JLabel("密码："));
        p2.add(tx_password);
        this.getContentPane().add(p2);
        // 添加按键事件
        bn_logion.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String account = tx_account.getText();
                String passward = tx_password.getText();
                if (account.equals("admin") && passward.equals("admin")) {
                    new JTableStudents(LoginFrame.this);//新建主窗口。
                } else {
                    JOptionPane.showMessageDialog(null, "用户名或密码错误！");
                }
            }
        });
        bn_reset.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                tx_account.setText("");
                tx_password.setText("");
            }
        });
        JPanel p3 = new JPanel();
        p3.add(bn_logion); // 添加按键
        p3.add(bn_reset);
        this.getContentPane().add(p3);

    }

    public static void main(String[] args) {
        LoginFrame frame = new LoginFrame();
        frame.setVisible(true); // 显示窗体
    }
}
